package com.example.jack.community.view.me.personinfo;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.percent.PercentRelativeLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jack.community.R;
import com.example.jack.community.view.me.BlackStatusActivity;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 用户二维码界面
 */
public class MyCodeActivity extends BlackStatusActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text_return)
    TextView textReturn;
    @BindView(R.id.filla)
    PercentRelativeLayout filla;
    @BindView(R.id.setting_user_logo)
    CircleImageView settingUserLogo;
    @BindView(R.id.setting_user_name)
    TextView settingUserName;
    @BindView(R.id.activity_my_code)
    PercentRelativeLayout activityMyCode;
    @BindView(R.id.img_user_code)
    ImageView imgUserCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.me_my_code);
        ButterKnife.bind(this);
        SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
        String tocken = sharedPreferences.getString("token", "tocken取出失败");
        generate(tocken);
    }

    @OnClick({R.id.back, R.id.text_return})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.text_return:
                finish();
                break;
        }
    }

    /**
     * 生成二维码
     */
    public void generate(String token) {
        Bitmap qrBitmap = generateBitmap(token, 700, 700);
        imgUserCode.setImageBitmap(qrBitmap);
    }

    /**
     * 生成二维码
     *
     * @param content 生成二维码的文字
     * @param width   生成的图片宽度
     * @param height  生成的文字高度
     * @return
     */
    private Bitmap generateBitmap(String content, int width, int height) {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    if (encode.get(j, i)) {
                        pixels[i * width + j] = 0x00000000;
                    } else {
                        pixels[i * width + j] = 0xffffffff;
                    }
                }
            }
            return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }


}
